package com.woniuxy.credit_loans.collection_zhang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.credit_loans.audit_li.dto.BonaDtoLi;
import com.woniuxy.credit_loans.collection_zhang.dto.PageDto;
import com.woniuxy.credit_loans.collection_zhang.service.BonaService;
import com.woniuxy.credit_loans.entity.Bona;
import com.woniuxy.credit_loans.entity.Borrow;
import com.woniuxy.credit_loans.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Bona")
public class BonaController {

    @Autowired
    private BonaService bonaService;

    @PostMapping("/page")
    public Result selectAll(@RequestBody PageDto pageDto){
        PageHelper.startPage(pageDto.getCurrentPage(),pageDto.getPagesize(),true);
        List<Bona> list = bonaService.selectAll(pageDto.getInput());
        PageInfo<Bona> pageInfo = new PageInfo<Bona>(list);
        return Result.ok().data("list",list).data("pageInfo",pageInfo.getTotal());

    }

    @PostMapping("")
    public  Result add(@RequestBody Bona bona){
         int i = bonaService.insertSelective(bona);
        if (i==0) {
            return Result.ok();
        }else {
            return  Result.error();
        }
    }

    @GetMapping("{id}")
    public  Result selectById(@PathVariable Long id){
         Bona bona = bonaService.selectByPrimaryKey(id);
        return  Result.ok().data("bona",bona);
    }

    @PutMapping("")
    public  Result update(@RequestBody Bona bona){
         int i = bonaService.updateByPrimaryKeySelective(bona);
        if (i==0) {
            return Result.ok();
        }else {
            return  Result.error();
        }
    }

    @DeleteMapping("{id}")
    public  Result delete(@PathVariable Long id){
         int i = bonaService.deleteByPrimaryKey(id);
        if (i==0) {
            return Result.ok();
        }else {
            return  Result.error();
        }
    }

    //添加改变初始信誉分
    @PostMapping("/bona")
    public Result addOne(@RequestBody BonaDtoLi bonaDtoLi){
        int i = bonaService.insertSelectiveByUserId(bonaDtoLi);
        return Result.ok();
    }
}
